﻿namespace com.stifel.BranchMgmt.WebParts.PromoteInfo
{
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;

    using com.stifel.BranchMgmt.WorkflowPromoteInfo;
    using com.stifel.BranchMgmtDao;
    using com.stifel.Extensions.SharePointExtensions;
    using com.stifel.Extensions.StringExtensions;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;

    public partial class PromoteInfoUserControl : UserControl
    {
        #region Fields

        bool uiBasics_Loaded;

        #endregion Fields

        #region Methods

        protected void Page_Load( object sender, EventArgs e )
        {
            hidePromotingMsgAndEnablePromote( );
            setUrlReferrer( );
            setDefaultView( );
        }

        protected void uiBasics_Activate( object sender, EventArgs e )
        {
            if ( !uiBasics_Loaded )
            {
                loadBasics( HttpContext.Current.Request.QueryString[ "ItemGuid" ].ToGuid( ) );
                uiBasics_Loaded = true;
            }
        }

        protected void uiBasics_Deactivate( object sender, EventArgs e )
        {
        }

        protected void uiBtnPromote_Click( object sender, EventArgs e )
        {
            Guid guid = HttpContext.Current.Request.QueryString[ "ItemGuid" ].ToGuid( );
            if ( isValidRequest( guid ) )
            {
                hidePromotingMsgAndEnablePromote( );
                if ( isNotPromoting( guid ) == true )
                {
                    showPromotingMsgAndDisablePromote( );
                    SPWeb web = SPContext.Current.Web;
                    SPUserInfoExpanded user = web.GetUserByLoginName( web.CurrentUser.LoginName );
                    WorkflowPromoteInfo.Start( guid, web, user );
                    setDefaultView( );
                }
                else
                {
                    showPromotingMsgAndDisablePromote( );
                }
            }
            else
            {
                uiInfo.Visible = false;
                uiMsg.Visible = true;
            }
        }

        protected void uiNavigationTab_Click( object sender, EventArgs e )
        {
            Button button = sender as Button;
            navigateToSelectedView( button );
        }

        protected void uiView_OnLoad( object sender, EventArgs e )
        {
            navigateToSelectedView( uiBtnBasics );
        }

        #endregion Methods
    }
}